(function($) {

	$.fn.pag = function(opt) {
		that = this;
		opt = opt || {};
		// how much
		var total = opt.total || 9; //
		var pageNum = opt.pageNum || 1; // default 1
		var pageSize = opt.pageSize || 9; //
		var maxPag = 6;

		var pages = function(t, s) {
			var r = t / s;
			var r2 = Math.round(r);
			return r > r2 ? r2 + 1 : r2;
		}(total, pageSize);

		var upPage = function(p, n) {
			return p > n ? n - 1 : 1;
		}(pages, pageNum);

		var downPage = function(p, n) {
			return p > n ? n + 1 : 1;
		}(pages, pageNum);

		// clean
		that.html("");
		// <li><a type="prev" href="#">上一页</a></li>
		// that.append("<li><a type=\"prev\"
		// href=\"javascript:window.reLoadGrid("+upPage+");\">上一页</li>");
		var lis_content = "<li><a type=\"prev\" href=\"javascript:window.reLoadGrid("
				+ upPage + ");\">上一页</li>";
		var index = 1;
		while (index < pages || index > maxPag) {
			// <li class="ch-pagination-current"><a href="#">1</a></li>
			var li_content = "<li ";
			if (index == pageNum)
				li_content += "class=\"ch-pagination-current\" ";
			li_content += "><a type=\"prev\" href=\"javascript:window.reLoadGrid("
					+ index + ");\">" + index + "</li>";
			// that.append(li_content);
			lis_content += li_content;
			index++;
		}
		// <li><a type="next" href="#">下一页</a></li>
		// that.append("<li><a type=\"prev\"
		// href=\"javascript:window.reLoadGrid("+downPage+");\">下一页</li>");
		lis_content += "<li><a type=\"prev\" href=\"javascript:window.reLoadGrid("
				+ downPage + ");\">下一页</li>";
		// console.log(that.html());
		that.html(lis_content);
	};

	/**
	 * 
	 * 
	 * @param opt
	 * @returns
	 */
	$.fn.grid = function(opt) {

		opt = opt || {};

		var that = this;
		var body = that.find("tbody");
		var maxSize = opt.maxSize || 9;
		// var curSize = body.find("tr").length ; //if grid's data is null ,
		// value is 0
		var curSize = getCurSize() || 0;
		// console.log(that);
		var url = opt.url || "";
		function getHeads() {
			var heads = that.find("thead tr th");
			var indexMap = [];
			for ( var i = 0, len = heads.length; i < len; i++) {
				var th = heads[i];
				if (th.id != "")
					indexMap.push(th.id);
			}
			return indexMap;
		}

		function getCurSize() {
			var rows = body.find("tr");
			for ( var i = 0, len = rows.length; i < len; i++) {
				// console.log($(rows[i]).attr("isnull"));
				if (!$(rows[i]).attr("isnull")
						|| $(rows[i]).attr("isnull") == "false")
					return i;
			}
			return rows.length;
		}

		that.fillGrid = function() {
			var rowNum = getCurSize();
			while (rowNum <= maxSize - 1) {
				// console.log(rowNum);
				that.addRow(null, rowNum);
				rowNum++;
			}
		};

		/**
		 * @rowData Object / Array
		 * @rowIndex number from 0
		 * @rowIndex string
		 */
		that.addRow = function(rowData, rowIndex) {
			rowIndex = rowIndex ? rowIndex : null || getCurSize();
			if (getCurSize() == maxSize)
				return;
			// think about fill datas
			// console.log(body);
			var tr_content = "<tr id=\"r" + rowIndex + "\" isNull=";
			tr_content += rowData != null ? "\"true\" >" : "\"false\" >";
			// if this row is null , then true
			if (rowData != null)
				tr_content += "<td id=\"td_chk_" + rowIndex
						+ "\"><input type=\"checkbox\" id=\"chk_" + rowIndex
						+ "\"/></td>";
			else
				tr_content += "<td></td>";
			rowData = rowData || {};
			for ( var i = 0, len = this.head.length; i < len; i++) {
				var hName = this.head[i];
				tr_content += "<td id=\"r" + rowIndex + "_" + hName + "\">";
				tr_content += rowData[hName] ? rowData[hName] : "";
				tr_content += "</td>";
			}
			tr_content += "</tr>";
			// body.append(tr_content);
			if (body.find("tr")[rowIndex])
				$(body.find("tr")[rowIndex]).replaceWith(tr_content);
			else
				body.append(tr_content);
			curSize = rowIndex;

			return that;
		};

		that.addDataSource = function(datas) {
			if (datas instanceof Array) {
				for ( var i = 0, len = datas.length; i < len; i++) {
					that.addRow(datas[i]);
				}
			}
		};

		that.clean = function() {
			$(body).html("");
			$(".ch-pagination").pag({
				total : 0 ,
				pageNum : 0,
				pageSize : 0
			});
			that.fillGrid();
		};

		that.head = opt.head || getHeads();

		if (url != "") {
			var infos = _util.synRequest({
				url : url,
				data : opt.data || {}
			});
			if (infos) {
				that.addDataSource(infos["datas"]); // fill data
			}
			// open pag
			$(opt.pag||".ch-pagination").pag({
				total : infos.total ,
				pageNum : infos.pageNum||1,
				pageSize : 9
			});
		}

		that.fillGrid();
		return that;
	};

})(jQuery);